#!/bin/sh
# Begin $RC_BASE/init.d/rc

# Get the configuration file
# All changes are to occur in the config file
. /etc/sysconfig/rc

# These 3 signals will not cause our script to exit
trap "" INT QUIT TSTP

# Simple sanity check - rc only takes one argument
if [ "${#}" -ne 1 ]; then
    echo "Usage: ${0} <runlevel>" >&2
    exit 1
fi

# Do not use the RUNLEVEL and PREVLEVEL variables provided by init so 
# that they can be modified and alternate directories (S) can 
# be used without affecting init
runlevel="${1}"
prevlevel="${PREVLEVEL}"

# Just in case - some flavors of init don't set PREVLEVEL to 'N'
if [ "${prevlevel}" = "" ]; then
    prevlevel="N"
fi

# Mount a tmpfs to store boot accounting information
if [ "${runlevel}" = "S" -a "${TEMPFS_MOUNT}" != "" ]; then
    mount -n -t tmpfs tmpfs "${TEMPFS_MOUNT}" -o mode=600
fi

# Provide an interactive prompt (if requested)
if [ "${runlevel}" = "S" -a "${iprompt}" = "yes" ]; then
    # ash does not accept t and n flags for read
    ls -l /bin/sh | grep "/ash"
    if [ "${?}" -eq "0" ]; then
        # We are using ash
        echo -e -n "${WARNING}WARNING:  Either bash or zsh is required"
        echo -e "${WARNING} for interactive startup.\n"
        sleep 3
    else
        echo ""
        # dcol and icol are spaces before the message to center the
        # message on screen.
        dcol=$(( $(( ${COLUMNS} - ${dlen} )) / 2 ))
        icol=$(( $(( ${COLUMNS} - ${ilen} )) / 2 ))
        echo -e "\\033[${dcol}G${welcome_message}"
        echo -e "\\033[${icol}G${i_message}${NORMAL}"
        echo ""
        read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
        if [ "${interactive}" = "I" -o "${interactive}" = "i" ]; then
            echo -n -e "${CURS_UP}"
            echo -e "${INFO}Interactive boot selected...${NORMAL}"
            echo "interactive=I" > "${TEMPFS_MOUNT}/.interactive-start"
        fi
    fi
fi


# Verify that the directory exists
if [ ! -d "${RC_BASE}/rc${runlevel}.d" ]; then
    echo -n -e "${WARNING}${RC_BASE}/rc${runlevel}.d does not exist."
    echo -e "${NORMAL}"
    exit 1
fi

# Source the interactive state file if it exists
if [ "${runlevel}" != "S" -a -f "${TEMPFS_MOUNT}/.interactive-start" ]; then
    . "${TEMPFS_MOUNT}/.interactive-start"
fi

# Prompt for interactive startup after completing S
if [ "${interactive}" = "I" -a "${runlevel}" != "S" -a \
    "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
    echo -n -e "Proceed with interactive starup of runlevel "
    echo -n -e "${INFO}${runlevel}${NORMAL}?"
    echo -n -e "(${FAILURE}y${NORMAL})es/(${FAILURE}n${NORMAL})o "
    read -n 1 go_on
    echo ""
    if [ "${go_on}" = "n" ]; then
        # don't continue
        exit 0
    fi
fi


# Attempt to stop all services started in the previous runlevel,
# that are stopped in this runlevel
if [ "${prevlevel}" != "N" ]; then
    for link in $(ls -v ${RC_BASE}/rc${runlevel}.d/K* 2> /dev/null)
    do
        # Check to see if link is a valid symlink
        if [ ! -f ${link} ]; then
                echo -e "${WARNING}${link} is not a valid symlink."
                continue # go on to the next K* link
        fi

        # Check to see if link is executable
        if [ ! -x ${link} ]; then
                echo -e "${WARNING}${link} is not executable, skipping."
                continue # go on to the next K* link
        fi

        script=${link#$RC_BASE/rc$runlevel.d/K[0-9][0-9]}
        prev_start=$RC_BASE/rc$prevlevel.d/S[0-9][0-9]$script
        S_start=$RC_BASE/rcS.d/S[0-9][0-9]$script

        if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
            if [ ! -f ${prev_start} ] && [ ! -f ${S_start} ]; then
                echo -e -n "${WARNING}WARNING:\n\n${link} can't be"
                echo -e "${WARNING} executed because it was not"
                echo -e -n "${WARNING} not started in the previous"
                echo -e "${WARNING} runlevel (${prevlevel})."
                echo -e "${NORMAL}"
                continue
            fi
        fi
        ${link} stop
        error_value=${?}

        if [ "${error_value}" != "0" ]; then
            print_error_msg
        fi
    done
fi

# Start all functions in this runlevel if they weren't started in
# the previous runlevel
for link in $( ls -v ${RC_BASE}/rc${runlevel}.d/S* 2> /dev/null)
do
    if [ "${prevlevel}" != "N" ]; then
        script=${link#$RC_BASE/rc$runlevel.d/S[0-9][0-9]}
        stop=$RC_BASE/rc$runlevel.d/K[0-9][0-9]$script
        prev_start=$RC_BASE/rc$prevlevel.d/S[0-9][0-9]$script

        [ -f ${prev_start} ] && [ ! -f ${stop} ] && continue
    fi

    # Check to see if link is a valid symlink
    if [ ! -f ${link} ]; then
        echo -e "${WARNING}${link} is not a valid symlink."
        continue # go on to the next K* link
    fi

    # Check to see if link is executable
    if [ ! -x ${link} ]; then
        echo -e "${WARNING}${link} is not executable, skipping."
        continue # go on to the next K* link
    fi

    case ${runlevel} in
        0|6)
            ${link} stop
        ;;

        *)
            if [ "${interactive}" = "I" -o "${interactive}" = "i" ]; then
                echo -e -n "${WARNING}Start ${INFO}${link} ${WARNING}?"
                echo -e -n "${NORMAL}(${FAILURE}y${NORMAL})es/(${FAILURE}n${NORMAL})o "
                read -n 1 startit 2>&1 > /dev/null
                echo ""
                if [ "${startit}" = "y" -o "${startit}" = "Y" ]; then
                    ${link} start
                else
                    echo -e -n "${WARNING}Not starting ${INFO}${link}"
                    echo -e "${WARNING}.${NORMAL}\n"
                fi
            else
                ${link} start
            fi
        ;;
    esac
    error_value=${?}

    if [ "${error_value}" -gt "1" ]; then
        print_error_msg
    fi
done

# Strip apply time to the logs, strip out any color codes and dump 
# the log to /var/log/boot.log
if [ -f "${TEMPFS_MOUNT}/.bootlog" -a "${runlevel}" != "S" ]; then
    # Remove any color codes from the temp log file
    sed -i 's@\\033\[[0-9];[0-9][0-9]m@@g' "${TEMPFS_MOUNT}/.bootlog"
    #Fix the time and hostname
    BTIMESPEC=$(echo `date +"%b %d %T"` `hostname`)
    sed -i "s@^bootlog:@${BTIMESPEC} bootlog:@" "${TEMPFS_MOUNT}/.bootlog"
    # Don't try and write in 0 and 6, this is a 'boot' log
    if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
        cat "${TEMPFS_MOUNT}/.bootlog" >> /var/log/boot.log
        rm -f "${TEMPFS_MOUNT}/.bootlog"
    fi
fi

# End $RC_BASE/init.d/rc
